﻿using System;
using System.Collections.Generic;
using System.Linq;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;

namespace JarvisProject1
{
    public sealed class MyCustomControl : Control
    {
        const double radius = 20;

        public static readonly DependencyProperty DataProperty =
               DependencyProperty.Register("Data", typeof(Geometry), typeof(MyCustomControl),
                   new PropertyMetadata(null));

        public Geometry Data
        {
            get { return (Geometry)GetValue(DataProperty); }
            set { SetValue(DataProperty, value); }
        }

        DispatcherTimer timer;
        double progress = 0.0;
        Size arrangeSize = new Size(0, 0);
        DateTime lastTime = DateTime.Now;

        public MyCustomControl()
        {
            this.DefaultStyleKey = typeof(MyCustomControl);
            timer = new DispatcherTimer();
            timer.Tick += timer_Tick;
            timer.Interval = TimeSpan.FromMilliseconds(5);
            timer.Start();
        }
        void timer_Tick(object sender, object e)
        {
            DateTime time = DateTime.Now;
            progress += 0.5 * (time - lastTime).TotalSeconds;
            lastTime = time;
            if (progress > 1)
                progress -= 1;
            Data = CreateGeometry(progress);
        }
        double CalcAngle(double progress)
        {
            double factor = 0.5 * Math.Cos(Math.PI * progress - Math.PI) + 0.5;
            return 2 * Math.PI * factor;
        }
        double CalcDistance(double angle)
        {
            return 0.3 * (Math.PI - Math.Abs(Math.PI - angle));
        }
        Geometry CreateGeometry(double progress)
        {
            double angle = CalcAngle(progress);
            double distance = CalcDistance(angle);
            GeometryGroup newGeometry = new GeometryGroup() { FillRule = FillRule.Nonzero };
            for (int i = 3; i > -4; i--)
            {
                Point location =
                    new Point(0.5 * arrangeSize.Width
                                         + radius * Math.Cos(angle + i * distance - Math.PI / 2),
                              0.5 * arrangeSize.Height
                                         + radius * Math.Sin(angle + i * distance - Math.PI / 2));
                newGeometry.Children.Add(new EllipseGeometry()
                {
                    Center = location,
                    RadiusX = 3,
                    RadiusY = 3
                });
            }
            return newGeometry;
        }
        protected override Size MeasureOverride(Size availableSize)
        {
            base.MeasureOverride(availableSize);
            return new Size(!double.IsInfinity(availableSize.Width) ? availableSize.Width : 0,
                !double.IsInfinity(availableSize.Height) ? availableSize.Height : 0);
        }
        protected override Size ArrangeOverride(Size arrangeBounds)
        {
            arrangeSize = arrangeBounds;
            return base.ArrangeOverride(arrangeBounds);
        }
    }
}
